using System;
using System.Collections.Generic;
using System.Windows.Media;
using WPFTemplateLib.Mvvm;
using Color = System.Windows.Media.Color;
using Point = System.Drawing.Point;

namespace WPFPractice.UserControls.DynamicColumnTable
{
	/// <summary>
	/// 单元格点击回调
	/// </summary>
	/// <param name="cell"></param>
	public delegate void CellClickCallback(TableCell cell);

	/// <summary>
	/// 单元格数据
	/// </summary>
	public class TableCell : SimpleBindableBase
	{
		public TableCell () { }

		public TableCell (object value)
		{
			Value = value;
		}

		/// <summary>
		/// 单元格默认背景色
		/// </summary>
		public static readonly Color CellDefaultColor = Colors.White;

		/// <summary>
		/// 单元格值修改事件
		/// </summary>
		public event EventHandler ValueChangedEvent;

		/// <summary>
		/// 类型
		/// </summary>
		public TableCellTypeEnum Type { set; get; } = TableCellTypeEnum.Label;

		/// <summary>
		/// 上限
		/// </summary>
		public object Upper { set; get; } = double.MaxValue;

		/// <summary>
		/// 下限
		/// </summary>
		public object Lower { set; get; } = double.MinValue;

		/// <summary>
		/// 默认值
		/// </summary>
		public object Default { set; get; }

		private object _Value;
		/// <summary>
		/// 取值
		/// </summary>
		public object Value
		{
			set
			{
				var flag = !EqualityComparer<object>.Default.Equals(_Value, value) && Type != TableCellTypeEnum.Label;
				SetProperty(ref _Value, value);
				if(flag)
				{
					ValueChangedEvent?.Invoke(this, EventArgs.Empty);
				}
			}
			get => _Value;
		}

		/// <summary>
		/// 选项列表
		/// </summary>
		public List<KeyValueModel> Options { set; get; }

		/// <summary>
		/// 名称
		/// </summary>
		public string Name { set; get; }

		/// <summary>
		/// 行列坐标（动态表格会自动计算）
		/// </summary>
		public Point Position { get; set; }

		/// <summary>
		/// 背景颜色
		/// </summary>
		public Color BackgroundColor { get; set; } = CellDefaultColor;

		/// <summary>
		/// 可存放一些数据
		/// </summary>
		public object Data { get; set; }

		/// <summary>
		/// 单元格点击回调。type为callback时，点击会触发
		/// </summary>
		public CellClickCallback ClickCallback;
	}
}
